package com.hs.telepass.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.hs.telepass.model.SingouProfiles;
import com.hs.telepass.model.TelepassProfiles;
import com.hs.telepass.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

public class SingouProfilesDatabase {
    private Context mContext;

    public static final String TABLE_NAME = "singouProfiles";
    public static final String COLUMN_KEY_ID = "profileID";
    public static final String COLUMN_SERVICE_NAME = "serviceName";
    public static final String COLUMN_SERVICE_ID = "serviceID";
    public static final String COLUMN_TITLE = "title";
    public static final String COLUMN_LOGO_URL = "logoURL";
    public static final String COLUMN_USERNAME = "username";
    public static final String COLUMN_ENCRYPTED_PASSWORD = "encryptedPassword";

    public SingouProfilesDatabase(Context mContext) {
        this.mContext = mContext;
    }

    public long addRecord(SingouProfiles profile) {
        List<SingouProfiles> recordtList = new ArrayList<SingouProfiles>();
        recordtList.add(profile);
        return addRecords(recordtList).get(0);
    }

    public List<Long> addRecords(List<SingouProfiles> recordList) {
        SQLiteDatabase db = DatabaseHelper.getInstance(mContext);
        List<Long> idList = new ArrayList<Long>();
        for (SingouProfiles profile : recordList) {
            ContentValues cv = new ContentValues();
            cv.put(COLUMN_SERVICE_NAME, profile.getServiceName());
            cv.put(COLUMN_SERVICE_ID, profile.getServiceID());
            cv.put(COLUMN_TITLE, profile.getTile());
            cv.put(COLUMN_LOGO_URL, profile.getLogoURL());
            cv.put(COLUMN_USERNAME, profile.getUsername());
            cv.put(COLUMN_ENCRYPTED_PASSWORD, profile.getEncrytedPassword());
            long id = db.insert(TABLE_NAME, null, cv);
            idList.add(id);
        }

        return idList;
    }

    public List<SingouProfiles> getProfiles() {
        SQLiteDatabase db = DatabaseHelper.getInstance(mContext);
        List<SingouProfiles> list = new ArrayList<SingouProfiles>();
        String sql = "select * from " + TABLE_NAME;
        Cursor cursor = db.rawQuery(sql, null);
        while(cursor.moveToNext()) {
            SingouProfiles profile = new SingouProfiles();
            int profileId = cursor.getInt(cursor.getColumnIndex(COLUMN_KEY_ID));
            String serviceName = cursor.getString(cursor.getColumnIndex(COLUMN_SERVICE_NAME));
            String serviceId = cursor.getString(cursor.getColumnIndex(COLUMN_SERVICE_ID));
            String title = cursor.getString(cursor.getColumnIndex(COLUMN_TITLE));
            String logoUrl = cursor.getString(cursor.getColumnIndex(COLUMN_LOGO_URL));
            String username = cursor.getString(cursor.getColumnIndex(COLUMN_USERNAME));
            String encryptedPassword = cursor.getString(cursor.getColumnIndex(COLUMN_ENCRYPTED_PASSWORD));

            profile.setProfileID(profileId);
            profile.setServiceName(serviceName);
            profile.setServiceID(serviceId);
            profile.setTile(title);
            profile.setLogoURL(logoUrl);
            profile.setUsername(username);
            profile.setEncrytedPassword(encryptedPassword);

            list.add(profile);
        }
        cursor.close();
        return list;
    }

    /**
     * 更新数据状态
     * */
    public void updateProfile(long id, String username, String password) {
        SQLiteDatabase db = DatabaseHelper.getInstance(mContext);

        ContentValues cv = new ContentValues();
        if(!StringUtil.isEmpty(username))
            cv.put(COLUMN_USERNAME, username);

        cv.put(COLUMN_ENCRYPTED_PASSWORD, password);

        db.update(TABLE_NAME, cv, COLUMN_KEY_ID + "=?",
                new String[] {String.valueOf(id) });
    }

    public int deleteProfile(int id){
        SQLiteDatabase db = DatabaseHelper.getInstance(mContext);
        String where = COLUMN_KEY_ID + "=?";
        String[] whereArgs = {String.valueOf(id)};
        return db.delete(TABLE_NAME, where, whereArgs);
    }



/*


    public void clearRecords(){
        SQLiteDatabase db = DatabaseHelper.getInstance(mContext);
        DatabaseHelper dh = DatabaseHelper.getHelper(mContext);
        dh.clearLoginTable(db);
    }

 */


}
